package com.fenghongzhang.day0102;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;

public class SMSObserver extends ContentObserver {

    private Context context;

    private Handler handler;
    public SMSObserver(Handler handler, Context context) {
        super(handler);
        this.handler = handler;
        this.context = context;
    }

    /**
     * 查询数据
     * @param selfChange
     */
    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
        if (query!= null){
            //所有
//            while(query.moveToNext()){
//
//            }
            if(query.moveToFirst()){
                String body = query.getString(query.getColumnIndex(Telephony.Sms.BODY));
                Message obtain = Message.obtain();
                obtain.obj = body;
                handler.sendMessage(obtain);
            }

            query.close();
        }

    }
}
